#include "napi/native_api.h"
#include <cstdio>
#include <cstdlib>
#include <hilog/log.h>
static napi_value Add(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;
}

static napi_value string2Hex(napi_env env, napi_callback_info cbinfo) {
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, cbinfo, &argc, args, nullptr, nullptr);
    size_t len;
    napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "inputSize-----------%{public}d", len);
    //     char *input = (char *)malloc(len + 1);
    char input[len + 1];
    napi_get_value_string_utf8(env, args[0], input, len + 1, &len);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "input-----------%{public}s", input);
    //    char str[] = u8"涂";
    int size = sizeof(input);
    unsigned char bytes[size];
    /* char hexString[64] = {""};*/
    char *hex = (char *)malloc(size*2);
    // 将字符串复制到字节数组
    for (int i = 0; input[i] != '\0'; i++) {
        bytes[i] = input[i];
    }

    // 打印字节数组
    for (int i = 0; input[i] != '\0'; i++) {
        // printf("%02x", bytes[i]);
        sprintf(hex + i * 2, "%02x", bytes[i]);
    }
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "hex-----------%{public}s", hex);
    napi_value result = nullptr;
    napi_create_string_utf8(env, hex, NAPI_AUTO_LENGTH, &result);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "result--hex-----------%{public}s", hex);
    free(hex);
    return result;
}

static napi_value obj(napi_env env, napi_callback_info cbInfo) {
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, cbInfo, &argc, args, nullptr, nullptr);
    char nameTemp[32];
    size_t len;
    int ageTemp;
    napi_get_value_string_utf8(env, args[0], nameTemp, 32, &len);
    napi_get_value_int32(env, args[1], &ageTemp);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "age-----------%{public}d", ageTemp);
    OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "testTag", "name-----------%{public}s", nameTemp);
    napi_value name, age;
    napi_create_string_utf8(env, nameTemp, len, &name);
    napi_create_int32(env, ageTemp, &age);
    napi_value resultObj = nullptr;
    napi_create_object(env, &resultObj);                   // 创建对象
    napi_set_named_property(env, resultObj, "name", name); // 赋值
    napi_set_named_property(env, resultObj, "age", age);   // 赋值
    return resultObj;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"string2Hex", nullptr, string2Hex, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"obj", nullptr, obj, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "ndk",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterNdkModule(void) { napi_module_register(&demoModule); }
